﻿Imports System.Linq

Public Class LogRepository
    Private dc As New TanNamChinhDataContext

    Public Sub New()
        dc = GeneralClassDAL.dc
    End Sub

    Public Function GetLogList(ByVal id_action As Integer, ByVal userName As String, ByVal logContent As String, ByVal fromDate As Date, ByVal toDate As Date) As IQueryable(Of tblLog)
        'Dim query = (From p In dc.tblLogs
        '            Select New With {p.LogDate, p.tblAction.ActionName, p.LogContent, p.tblUser.UserName, p.Ip}).OrderByDescending(Function(p) p.LogDate)
        Dim query = From p In dc.tblLogs
                    Select p
        If id_action > 0 Then
            query = query.Where(Function(p) p.ID_Action = id_action)
        End If

        If userName <> "" Then
            query = query.Where(Function(p) p.tblUser.UserName.ToLower().StartsWith(userName))
        End If

        If logContent <> "" Then
            query = query.Where(Function(p) p.LogContent.ToLower().Contains(logContent))
        End If

        If logContent <> "" Then
            query = query.Where(Function(p) p.LogContent.ToLower().Contains(logContent))
        End If

        query = query.Where(Function(p) p.LogDate >= fromDate And p.LogDate <= toDate)

        query = query.OrderByDescending(Function(p) p.LogDate)
        Return query
    End Function

    Public Function GetLog(ByVal id As Integer) As tblLog
        Dim Log = dc.tblLogs.SingleOrDefault(Function(p As tblLog) p.ID_Log = id)
        Return Log
    End Function

    Public Sub Insert(ByVal obj As tblLog)
        dc.tblLogs.InsertOnSubmit(obj)
        dc.SubmitChanges()
    End Sub

    Public Sub DeleteAll()
        dc.ExecuteCommand("TRUNCATE TABLE tblLog")
    End Sub
End Class
